package com.jah.picavue.client.model;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONValue;
import com.jah.picavue.client.Config;

public class Photo implements Config{
    private String PhotoID;
    private String PhotoThumb;
    private String PhotoCaption;
    private String PhotoContent;
    private Double PhotoWidth;
    private Double PhotoHeight;

    private Double OriginalWidth;
    private Double OriginalHeight;

    public Photo() {};

    // FIXME - much better checking!
    public Photo(JSONValue entry) {
    	setID(entry.isObject().get("gphoto$id").isObject().get("$t").isString().stringValue());
    	JSONArray thumbs = entry.isObject().get("media$group").isObject().get("media$thumbnail").isArray();
        JSONValue t = null;
        // Get the required size thumbnail
        for (int i = 0; i < thumbs.size(); i++) {
        	Double h = thumbs.get(i).isObject().get("height").isNumber().doubleValue();
        	Double w = thumbs.get(i).isObject().get("width").isNumber().doubleValue();
        	if ( h*w <= Math.pow(THUMBSIZE,2) && (h == THUMBSIZE || w == THUMBSIZE) ) {
        		t = thumbs.get(i);
        		break;
        	}
    	}
    	if (t != null) setThumb(t.isObject().get("url").isString().stringValue());
    	// Get the caption
    	if (entry.isObject().get("summary").isObject() != null) {
    		setCaption(entry.isObject().get("summary").isObject().get("$t").isString().stringValue());
    	}
    	// Get URL to the large image (maximum longest size is 800px)
    	// FIXME don't assume that the required image is the first member of the media$content array.
    	setContent(entry.isObject().get("media$group").isObject().get("media$content").isArray().get(0).isObject().get("url").isString().stringValue());
    	// Get the dimensions of the large image.
    	setWidth(entry.isObject().get("media$group").isObject().get("media$content").isArray().get(0).isObject().get("width").isNumber().doubleValue());
    	setHeight(entry.isObject().get("media$group").isObject().get("media$content").isArray().get(0).isObject().get("height").isNumber().doubleValue());

    	this.OriginalHeight = this.PhotoHeight;
    	this.OriginalWidth = this.PhotoWidth;
    };

    public void setID(String id) {
    	this.PhotoID = id;
    }

    public void setThumb(String path) {
    	this.PhotoThumb = path;
    }

    public void setCaption(String caption) {
    	this.PhotoCaption = caption;
    }

    public void setContent(String path) {
    	this.PhotoContent = path;
    }

    public void setWidth(Double w) {
    	this.PhotoWidth = w;
    }

    public void setHeight(Double h) {
    	this.PhotoHeight = h;
    }

    public String getID() {
    	return this.PhotoID;
    }

    public String getThumb() {
    	return this.PhotoThumb;
    }

    public String getCaption() {
    	return this.PhotoCaption;
    }

    public String getContent() {
    	return this.PhotoContent;
    }

    public Double getWidth() {
    	return this.PhotoWidth;
    }

    public Double getHeight() {
    	return this.PhotoHeight;
    }

    public void resetDims() {
    	setWidth(this.OriginalWidth);
    	setHeight(this.OriginalHeight);
    }
}
